package com.example.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.Constants;
import com.example.common.Result;
import com.example.entry.Rider;
import com.example.service.IRiderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/*
* 骑手后台管理控制器
* */
@RequestMapping("/admin/rider")
@RestController
public class RiderController {

    @Autowired
    private IRiderService riderService;

    /*
    * 骑手注册
    * */
    @PostMapping("/rider-register")
    public Result riderRegister(@RequestBody Rider rider){
        riderService.saveOrUpdate(rider);
        return Result.success();
    }

    /*
     * 骑手登录
     * */
    @PostMapping("/rider-login")
    public Result riderLogin(@RequestBody Rider rider){
        QueryWrapper<Rider> wrapper = new QueryWrapper<>();
        wrapper.eq("id", rider.getId());
        Rider riderInfo = riderService.getById(wrapper);
        if (riderInfo.getStatus() == 1) {
            return Result.error(Constants.CODE_400,"该账号以禁用");
        }
        return Result.success(riderInfo);
    }

    /*
     * 根据id查询信息
     * */
    @GetMapping("/rider-id={id}")
    public Result getRiderOne(@PathVariable Integer id){
        Rider byId = riderService.getById(id);
        return Result.success(byId);
    }

    /*
     * 根据id进行删除
     * */
    @GetMapping("/delete/id={id}")
    public Result deleteRiderInfo(@PathVariable Integer id) {
        riderService.removeById(id);
        return Result.success("删除成功");
    }

    /*
     * 批量删除
     * */
    @PostMapping("/batchd_delete")
    public Result batchDelete(@RequestBody List<Integer> ids){
        riderService.removeBatchByIds(ids);
        return Result.success("删除成功");
    }

    /*
     * 修改或保存信息
     * */
    @PostMapping("/update")
    public Result updateOrSavRider(@RequestBody Rider rider){
        riderService.saveOrUpdate(rider);
        return Result.success("更新成功");
    }

    /*
    * 骑手分页查询
    * */
    @GetMapping("/page")
    public Result getRiderPage(@RequestParam Integer pageNum,
                               @RequestParam Integer pageSize,
                               @RequestParam(defaultValue = "") String ridername,
                               @RequestParam(defaultValue = "") String riderphone,
                               @RequestParam(defaultValue = "0") Integer status,
                               @RequestParam(defaultValue = "" ) @DateTimeFormat(pattern = "yyyy-MM-dd") Date startTime,
                               @RequestParam(defaultValue = "") @DateTimeFormat(pattern = "yyyy-MM-dd") Date endTime
    ) {
        QueryWrapper<Rider> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("id");
        wrapper.eq("status",status);
        if(!"".equals(ridername)){
            wrapper.like("ridername", ridername);
        }
        if(!"".equals(riderphone)){
            wrapper.like("riderphone", riderphone);
        }
        if(Objects.nonNull(startTime) && Objects.nonNull(endTime)){
            wrapper.between("create_time",startTime, endTime);
        }
        Page<Rider> riderPage = riderService.page(new Page<>(pageNum, pageSize), wrapper);
        return Result.success(Constants.CODE_200, riderPage);
    }
}
